********************************************************************************
***                                Preamble                                  ***
********************************************************************************
* Project: Counter-Punishment
* Procedure: Figures
* version 15
clear all
macro drop _all
********************************************************************************

********************************************************************************
				*** STAGES 1 & 2: TAKE & PUNISH AVERAGE ***
********************************************************************************
use counterpunish.dta, clear
collapse take0 take5 take10 punish0 punish5 punish10  punish_prob0 punish_prob5 ///
	punish_prob10 if type==1, by(treat)	 

gen id=_n	
reshape long take punish punish_prob, i(id) j(taking)

gen take_level = take * 100
gen punish_likely = punish_prob * 100

*********  FIGURES *********
// For the Bar Graph only
drop if taking==0
clonevar bar_treat = treat
clonevar bar_taking = taking
replace bar_taking = cond(treat==2, bar_taking - 0.5, bar_taking + 0.5)
replace bar_treat = cond(treat==2, bar_taking - 0.5, bar_taking + 0.5)
******* COLOR SCHEME *******
graph set window fontface default
set scheme s2colormanu
global color2plight = `""0 0 0 %85""' 
global color3plight = `""252 186 3 %95""' 
global color2p = `""0 0 0""' 
global color3p = `""212 159 0""' 
global color3p2 = `""0 131 143""'
****************************
	
// TAKE + SIZE & LIKELIHOOD OF PUNISHMENT (Main Text)
twoway 	(bar take_level bar_taking if treat==1, fcolor($color2plight) lwidth(none) barwidth(.98)) ///
	(bar take_level bar_taking if treat==2, fcolor($color3plight) lwidth(none) barwidth(.98)) ///
	(connected punish taking if treat==2, mcolor($color3p) msize(medium) ///
	msymbol(circle) lcolor($color3p) lwidth(medthick) lpattern(solid) connect(ascending)) ///		
	(connected punish taking if treat==1, mcolor($color2p) msize(medium) ///
	msymbol(triangle) lcolor($color2p) lwidth(medthick) lpattern(solid) connect(ascending)) ///	
	(connected punish_likely taking if treat==2, mcolor($color3p) msize(medium) ///
	msymbol(circle) lcolor($color3p) lwidth(medthick) lpattern(dash) connect(ascending)) ///		
	(connected punish_likely taking if treat==1, mcolor($color2p) msize(medium) ///
	msymbol(triangle) lcolor($color2p) lwidth(medthick) lpattern(dash) connect(ascending)) ///	
	, ytitle(Frequency of take - Level / probability of punishment) ytitle(, size(medsmall)) ///
	yscale(noline) ylabel(0(10)100, labsize(medsmall) angle(horizontal) tlength(zero) ///
	glcolor(gs12) gmin gmax) xtitle(Amount taken) xtitle(, size(medsmall)) xscale(noline) ///
	xlabel(5(5)10, labsize(medsmall) tlength(zero)) ///
	title("{bf:B.} Punishment by amount taken", position(12) span size(medlarge) ///
	color(black)) subtitle("") caption("") note("") ///
	legend(order(2 "{bf:3P} TAKE-Frequency" 1 "{bf:2P} TAKE-Frequency" 3 "{bf:3P} PUN-Level" ///
	4 "{bf:2P} PUN-Level" 5 "{bf:3P} PUN-Choice" 6 "{bf:2P} PUN-Choice") ///
	rows(3) size(small) nobox region(lwidth(none)) position(12) ring(0)) ///
	plotregion(style(none)) graphregion(style(none)) subtitle(" ", nobox size(large) color(black))  ///	
	name(fig_pun, replace) nodraw
	
		
// BARS ON PUNISHMENT ONLY	
use counterpunish.dta, clear
keep treat group type take0 take5 take10 punish0 punish5 punish10 
keep if type==1

gen id=_n	
reshape long punish, i(id) j(take)

drop take0 take5 take10
drop if take==0

collapse (mean) meanpunish=punish (sd) sdpunish=punish (count) n=punish if type==1, by(take treat)

generate hipunish = meanpunish + invttail(n-1,0.025)*(sdpunish / sqrt(n))
generate lowpunish = meanpunish - invttail(n-1,0.025)*(sdpunish / sqrt(n))

*********  FIGURES *********
clonevar bar_take = take
replace bar_take = cond(treat==2, bar_take - 1.02, bar_take + 1.02)
graph set window fontface default
******* COLOR SCHEME *******
set scheme s2colormanu
global color2plight = `""0 0 0 %85""' 
global color3plight = `""252 186 3""' 
global color2p = `""0 0 0""' 
global color3p = `""212 159 0""' 
global color3plight2 = `""0 131 143 %95""'
global color3p2 = `""0 131 143""'
global color2p2 = `""55 71 79""' 
****************************

twoway (bar meanpunish bar_take if treat==1, fcolor($color3p2) lwidth(none) barwidth(2)) ///
	(bar meanpunish bar_take if treat==2, fcolor($color3plight) lwidth(none) barwidth(2)) ///
	(rcap hipunish lowpunish bar_take, lcolor($color2p2)) ///
	, ytitle(Mean amount of punishment) ytitle(, size(medsmall)) ///
	yscale(noline) ylabel(0(10)50, labsize(medsmall) angle(horizontal) tlength(zero) ///
	glcolor(gs12) gmin gmax) xtitle(Amount taken) xtitle(, size(medsmall)) xscale(noline) ///
	xlabel(5(5)10, labsize(medsmall) tlength(zero)) ///
	title("", position(12) span size(medlarge) ///
	color(black)) subtitle("") caption("") note("") ///
	legend(order(2 "{bf:3P}" 1 "{bf:2P}") rows(2) ///
	size(small) nobox region(lwidth(none)) position(11) yoffset(-2) ring(0)) ///
	plotregion(style(none)) graphregion(style(none)) subtitle("", nobox size(large) color(black))  ///	
	name(fig_pun_only, replace) 
			
********************************************************************************
			*** STAGES 1 & 2: TAKE & PUNISH LEVEL (NO ZEROES) ***
********************************************************************************
use counterpunish.dta, clear

forvalues i=0(5)10{
	replace punish`i'=. if punish`i'==0	
}

collapse take0 take5 take10 punish0 punish5 punish10  punish_prob0 punish_prob5 ///
	punish_prob10 if type==1, by(treat)	 
	
gen id=_n	
reshape long take punish punish_prob, i(id) j(taking)

gen take_level = take * 100
gen punish_likely = punish_prob * 100

*********  FIGURES *********
drop if taking==0
clonevar bar_taking = taking
replace bar_taking = cond(treat==2, bar_taking - 0.5, bar_taking + 0.5)
******* COLOR SCHEME *******
graph set window fontface default
set scheme s2colormanu
global color2plight = `""0 0 0 %85""' 
global color3plight = `""252 186 3 %95""' 
global color2p = `""0 0 0""' 
global color3p = `""212 159 0""' 
****************************
	
// TAKE + SIZE & LIKELIHOOD OF PUNISHMENT (Appendix)
twoway 	(bar take_level bar_taking if treat==1, fcolor($color2plight) lwidth(none) barwidth(1)) ///
	(bar take_level bar_taking if treat==2, fcolor($color3plight) lwidth(none) barwidth(1)) ///
	(connected punish taking if treat==2, mcolor($color3p) msize(medium) ///
	msymbol(circle) lcolor($color3p) lwidth(medthick) lpattern(solid) connect(ascending)) ///		
	(connected punish taking if treat==1, mcolor($color2p) msize(medium) ///
	msymbol(triangle) lcolor($color2p) lwidth(medthick) lpattern(solid) connect(ascending)) ///	
	, ytitle(Frequency of take - Level of punishment) ytitle(, size(medsmall)) ///
	yscale(noline) ylabel(0(10)100, labsize(medsmall) angle(horizontal) tlength(zero) ///
	glcolor(gs12) gmin gmax) xtitle(Amount taken) xtitle(, size(medsmall)) xscale(noline) ///
	xlabel(5(5)10, labsize(medsmall) tlength(zero)) ///
	title("{bf:B.} Punishment by amount taken", position(12) span size(medlarge) ///
	color(black)) subtitle("") caption("") note("") ///
	legend(order(2 "{bf:3P} TAKE-Frequency" 1 "{bf:2P} TAKE-Frequency" 3 "{bf:3P} PUN-Level" ///
	4 "{bf:2P} PUN-Level") rows(3) size(small) nobox region(lwidth(none)) position(12) ring(0)) ///
	plotregion(style(none)) graphregion(style(none)) subtitle(" ", nobox size(large) color(black))  ///	
	name(fig_pun_severe, replace) nodraw
		
********************************************************************************
					*** STAGE 3: COUNTER-PUNISH AVERAGE ***
********************************************************************************
use counterpunish.dta, clear
	
forvalues i=0(10)100{	
	gen counter_likely`i'=	counter_prob`i'*100
}

collapse counter* if type==1, by(treat)	 
gen id=_n	
reshape long counter counter_prob counter_likely, i(id) j(punish)

*********  FIGURES *********
clonevar bar_punish = punish
replace bar_punish = cond(treat==2, bar_punish - 2, bar_punish + 2)
******* COLOR SCHEME *******
graph set window fontface default
set scheme s2colormanu
global color2plight = `""0 0 0 %85""' 
global color3plight = `""252 186 3 %95""' 
global color2p = `""0 0 0""' 
global color3p = `""212 159 0""' 
****************************

drop if bar_punish<=2 // To take out cases where punishment was zero

// SIZE & LIKELIHOOD OF COUNTER-PUNISHMENT (Main Text)
twoway (bar counter bar_punish if treat==1, fcolor($color2plight) lwidth(none) barwidth(4)) ///
	(bar counter bar_punish if treat==2, fcolor($color3plight) lwidth(none) barwidth(4)) ///
	(connected counter_likely punish if treat==2, mcolor($color3p) msize(medium) ///
	msymbol(circle) lcolor($color3p) lwidth(medthick) lpattern(solid) connect(ascending))	///		
	(connected counter_likely punish if treat==1, mcolor($color2p) msize(medium) ///
	msymbol(triangle) lcolor($color2p) lwidth(medthick) lpattern(solid) connect(ascending)) ///
	, ytitle(Level / probability of counter-punishment) ytitle(, size(medsmall)) yscale(noline) ylabel(0(10)100, ///
	labsize(medsmall) angle(horizontal) tlength(zero) glcolor(gs12) gmin gmax)  ///
	xtitle(Level of punishment) xtitle(, size(medsmall)) xscale(noline) ///
	xlabel(10(10)100, labsize(medsmall) tlength(zero)) ///
	title("{bf:A.} Counterpunishment by level of punishment", position(12) span ///
	size(medlarge) color(black)) subtitle("") caption("") note("") ///
	legend(order(2 "{bf:3P} CPUN-Level" 1 "{bf:2P} CPUN-Level" 3 "{bf:3P} CPUN-Choice" ///
	4 "{bf:2P} CPUN-Choice" ) rows(4) size(small) nobox region(lwidth(none)) position(11) ring(0)) ///
	plotregion(style(none)) graphregion(style(none)) subtitle(" ", nobox size(large) color(black))  ///
	name(fig_cpun, replace)	nodraw
	
graph combine fig_cpun fig_pun, iscale(*1.25) imargin(sides) ///
	xsize(15) ysize(6) graphregion(margin(tiny) style(none)) ///
	name(fig_cpun_pun, replace)
		quietly graph export "fig_cpun_pun.png", replace width(2048)	
	
	
// BARS ON COUNTER-PUNISHMENT ONLY	
use counterpunish.dta, clear
keep treat group take type punish0 punish5 punish10 counter0 counter10 counter20 counter30 counter40 counter50 counter60 counter70 counter80 counter90 counter100

gen id=_n	
reshape long counter, i(id) j(punish)
drop punish0 punish5 punish10
drop if punish==0

collapse (mean) meancounter=counter (sd) sdcounter=counter (count) n=counter if type==1, by(punish treat)

generate hicounter = meancounter + invttail(n-1,0.025)*(sdcounter / sqrt(n))
generate lowcounter = meancounter - invttail(n-1,0.025)*(sdcounter / sqrt(n))

*********  FIGURES *********
clonevar bar_punish = punish
replace bar_punish = cond(treat==2, bar_punish - 2.1, bar_punish + 2.1)
******* COLOR SCHEME *******
graph set window fontface default
set scheme s2colormanu
global color2plight = `""0 0 0 %85""' 
global color3plight = `""252 186 3""' 
global color2p = `""0 0 0""' 
global color3p = `""212 159 0""' 
global color3plight2 = `""0 131 143 %95""'
global color3p2 = `""0 131 143""'
global color2p2 = `""55 71 79""' 
****************************

twoway (bar meancounter bar_punish if treat==1, fcolor($color3p2) lwidth(none) barwidth(4)) ///
	(bar meancounter bar_punish if treat==2, fcolor($color3plight) lwidth(none) barwidth(4)) ///
	(rcap hicounter lowcounter bar_punish, lcolor($color2p2)) ///
	, ytitle(Mean amount of counter-punishment) ytitle(, size(medsmall)) yscale(noline) ylabel(0(25)100, ///
	labsize(medsmall) angle(horizontal) tlength(zero) glcolor(gs12) gmin gmax)  ///
	xtitle(Level of punishment) xtitle(, size(medsmall)) xscale(noline) ///
	xlabel(10(10)100, labsize(medsmall) tlength(zero)) ///
	title("", position(12) span ///
	size(medlarge) color(black)) subtitle("") caption("") note("") ///
	legend(order(2 "{bf:3P}" 1 "{bf:2P}") rows(2) ///
	size(small) nobox region(lwidth(none)) position(11) yoffset(-2) ring(0)) ///
	plotregion(style(none)) graphregion(style(none)) subtitle("", nobox size(large) color(black))  ///
	name(fig_cpun_onlyx, replace)	
		quietly graph export "fig_cpun_onlyx.png", replace width(2048)	


replace lowcounter=0 if lowcounter<0

twoway (bar meancounter bar_punish if treat==1, fcolor($color3p2) lwidth(none) barwidth(4)) ///
	(bar meancounter bar_punish if treat==2, fcolor($color3plight) lwidth(none) barwidth(4)) ///
	(rcap hicounter lowcounter bar_punish, lcolor($color2p2)) ///
	, ytitle(Mean amount of counter-punishment) ytitle(, size(medsmall)) yscale(noline) ylabel(0(25)100, ///
	labsize(medsmall) angle(horizontal) tlength(zero) glcolor(gs12) gmin gmax)  ///
	xtitle(Level of punishment) xtitle(, size(medsmall)) xscale(noline) ///
	xlabel(10(10)100, labsize(medsmall) tlength(zero)) ///
	title("", position(12) span ///
	size(medlarge) color(black)) subtitle("") caption("") note("") ///
	legend(order(2 "{bf:3P}" 1 "{bf:2P}") rows(2) ///
	size(small) nobox region(lwidth(none)) position(11) yoffset(-2) ring(0)) ///
	plotregion(style(none)) graphregion(style(none)) subtitle("", nobox size(large) color(black))  ///
	name(fig_cpun_only, replace)	

********************************************************************************
				*** STAGE 3: COUNTER-PUNISH LEVEL (NO ZEROES) ***
********************************************************************************
use counterpunish.dta, clear

forvalues i=0(10)100{
	replace counter`i'=. if counter`i'==0
}

collapse counter* if type==1, by(treat)	 
gen id=_n	
reshape long counter, i(id) j(punish)

*********  FIGURES *********
clonevar bar_punish = punish
replace bar_punish = cond(treat==2, bar_punish - 2, bar_punish + 2)
******* COLOR SCHEME *******
graph set window fontface default
set scheme s2colormanu
global color2plight = `""0 0 0 %85""' 
global color3plight = `""252 186 3 %95""' 
global color2p = `""0 0 0""' 
global color3p = `""212 159 0""' 
****************************

drop if bar_punish<=2 // To take out cases where punishment was zero

// SEVERITY OF COUNTER-PUNISHMENT (Appendix)
twoway (bar counter bar_punish if treat==1, fcolor($color2plight) lwidth(none) barwidth(4)) ///
	(bar counter bar_punish if treat==2, fcolor($color3plight) lwidth(none) barwidth(4)) ///
	, ytitle(Level of counter-punishment) ytitle(, size(medsmall)) yscale(noline) ylabel(0(10)100, ///
	labsize(medsmall) angle(horizontal) tlength(zero) glcolor(gs12) gmin gmax)  ///
	xtitle(Level of punishment) xtitle(, size(medsmall)) xscale(noline) xlabel(10(10)100, ///
	labsize(medsmall) tlength(zero)) ///
	title("{bf:A.} Counterpunishment by level of punishment", position(12) span ///
	size(medlarge) color(black)) subtitle("") caption("") note("") ///
	legend(order(2 "{bf:3P} CPUN-Level" 1 "{bf:2P} CPUN-Level") rows(2) ///
	size(small) nobox region(lwidth(none)) position(11) ring(0)) ///
	plotregion(style(none)) graphregion(style(none)) subtitle(" ", nobox size(large) color(black))  ///
	name(fig_cpun_severe, replace)	nodraw

graph combine fig_cpun_severe fig_pun_severe, iscale(*1.25) imargin(sides) ///
	name(fig_pun_cpun_append, replace) xsize(15) ysize(6) rows(1) graphregion(margin(tiny) style(none))
				
				
********************************************************************************
				*** QUALTRIX SURVEY: REPORTS ON 2P BIAS ***
********************************************************************************
clear
	use cpun_survey.dta, clear
		
******* COLOR SCHEME *******
graph set window fontface default
set scheme s2colormanu
global color2plight = `""0 0 0 %85""' 
global color3plight = `""252 186 3""' 
global color2p = `""0 0 0""' 
global color3p = `""212 159 0""' 
global color3plight2 = `""0 131 143 %95""'
global color3p2 = `""0 131 143""'
global color2p2 = `""55 71 79""' 
****************************
	
collapse bias1 bias2 bias3 unbiased1 unbiased2 unbiased3
	
graph bar (mean) bias1 (mean) bias2 (mean) bias3, ///
	bar(1, fcolor($color2p2) lwidth(none)) ///
	bar(2, fcolor($color3plight) lwidth(none)) ///
	bar(3, fcolor($color3p2) lwidth(none)) ///
	bargap(1) blabel(bar, size(medsmall) format(%9.2f)) ///
	ytitle(Proportionof responses) ytitle(, size(medsmall)) yscale(noline) ///
	ylabel(0(.25)1, labsize(medsmall) angle(horizontal) format(%9.2f) tlength(zero) glcolor(gs12) gmin gmax) ///
	title("{bf:A.} How biased is second-party punishment?", position(12) span size(medlarge) color(black)) ///
	subtitle("") caption("") note(" ") ///
	legend(order(1 "Biased" 2 "Neutral" 3 "Unbiased") rows(3) ///
	size(small) nobox region(lwidth(none)) position(2) yoffset(-4) ring(0)) ///
	plotregion(style(none)) graphregion(style(none)) subtitle(" ", nobox size(large) color(black))  ///
	name(fig_q1_paper, replace) nodraw
	
graph bar (mean) unbiased1 (mean) unbiased2 (mean) unbiased3, ///
	bar(1, fcolor($color2p2) lwidth(none)) ///
	bar(2, fcolor($color3plight) lwidth(none)) ///
	bar(3, fcolor($color3p2) lwidth(none)) ///
	bargap(1) blabel(bar, size(medsmall) format(%9.2f)) ///
	ytitle(Proportionof responses) ytitle(, size(medsmall)) yscale(noline) ///
	ylabel(0(.25)1, labsize(medsmall) angle(horizontal) format(%9.2f) tlength(zero) glcolor(gs12) gmin gmax) ///
	title("{bf:B.} Is one of the two types of punishment more biased?", position(12) span size(medlarge) color(black)) ///
	subtitle("") caption("") note(" ") ///
	legend(order(1 "2nd party" 2 "Equal" 3 "3rd party") rows(3) ///
	size(small) nobox region(lwidth(none)) position(2) yoffset(-4) ring(0)) ///	
	plotregion(style(none)) graphregion(style(none)) subtitle(" ", nobox size(large) color(black))  ///
	name(fig_q4_paper, replace)	nodraw	
		
graph combine fig_q1_paper fig_q4_paper, iscale(*1.25) imargin(sides) ///
	xsize(15) ysize(6) graphregion(margin(tiny) style(none)) ///
	name(fig_survey_bias, replace)
		quietly graph export "fig_survey_bias.png", replace width(2048)	
	
	
****************************
*** APPENDIX NEW FIGURES ***
****************************

// BARS ON COUNTER-PUNISHMENT BY TAKE	
use counterpunish.dta, clear
keep treat group take type punish0 punish5 punish10 counter0 counter10 counter20 counter30 counter40 counter50 counter60 counter70 counter80 counter90 counter100

gen id=_n	
reshape long counter, i(id) j(punish)
drop punish0 punish5 punish10

collapse (mean) meancounter=counter (sd) sdcounter=counter (count) n=counter if type==1, by(punish take treat)

generate hicounter0 = meancounter + invttail(n-1,0.025)*(sdcounter / sqrt(n)) if take==0
	replace hicounter0=100 if hicounter0>100

generate lowcounter0 = meancounter - invttail(n-1,0.025)*(sdcounter / sqrt(n)) if take==0
	replace lowcounter0=0 if lowcounter0<0

generate hicounter5 = meancounter + invttail(n-1,0.025)*(sdcounter / sqrt(n)) if take==5
	replace hicounter5=100 if hicounter5>100
	
generate lowcounter5 = meancounter - invttail(n-1,0.025)*(sdcounter / sqrt(n)) if take==5
	replace lowcounter5=0 if lowcounter5<0

generate hicounter10 = meancounter + invttail(n-1,0.025)*(sdcounter / sqrt(n)) if take==10
	replace hicounter10=100 if hicounter10>100
	
generate lowcounter10 = meancounter - invttail(n-1,0.025)*(sdcounter / sqrt(n)) if take==10
	replace lowcounter10=0 if lowcounter10<0

*********  FIGURES *********
// For the Bar Graph only
clonevar bar_punish = punish
replace bar_punish = cond(treat==2, bar_punish - 2.1, bar_punish + 2.1)
******* COLOR SCHEME *******
graph set window fontface default
set scheme s2colormanu
global color2plight = `""0 0 0 %85""' 
global color3plight = `""252 186 3""' 
global color2p = `""0 0 0""' 
global color3p = `""212 159 0""' 
global color3plight2 = `""0 131 143 %95""'
global color3p2 = `""0 131 143""'
global color2p2 = `""55 71 79""' 
****************************

twoway (bar meancounter bar_punish if treat==1 & take==0, fcolor($color3p2) lwidth(none) barwidth(4)) ///
	(bar meancounter bar_punish if treat==2 & take==0, fcolor($color3plight) lwidth(none) barwidth(4)) ///
	(rcap hicounter0 lowcounter0 bar_punish, lcolor($color2p2)) ///
	, ytitle(Mean amount of counter-punishment) ytitle(, size(medsmall)) yscale(noline) ylabel(0(25)100, ///
	labsize(medsmall) angle(horizontal) tlength(zero) glcolor(gs12) gmin gmax)  ///
	xtitle(Level of punishment) xtitle(, size(medsmall)) xscale(noline) ///
	xlabel(0(10)100, labsize(medsmall) tlength(zero)) ///
	title("Take 0", position(12) span ///
	size(medlarge) color(black)) subtitle("") caption("") note("") ///
	legend(order(2 "{bf:3P}" 1 "{bf:2P}") rows(2) ///
	size(small) nobox region(lwidth(none)) position(11) yoffset(-2) ring(0)) ///
	plotregion(style(none)) graphregion(style(none)) subtitle("", nobox size(large) color(black))  ///
	name(fig_cpun_take0, replace)	
	
twoway (bar meancounter bar_punish if treat==1 & take==5, fcolor($color3p2) lwidth(none) barwidth(4)) ///
	(bar meancounter bar_punish if treat==2 & take==5, fcolor($color3plight) lwidth(none) barwidth(4)) ///
	(rcap hicounter5 lowcounter5 bar_punish, lcolor($color2p2)) ///
	, ytitle(Mean amount of counter-punishment) ytitle(, size(medsmall)) yscale(noline) ylabel(0(25)100, ///
	labsize(medsmall) angle(horizontal) tlength(zero) glcolor(gs12) gmin gmax)  ///
	xtitle(Level of punishment) xtitle(, size(medsmall)) xscale(noline) ///
	xlabel(0(10)100, labsize(medsmall) tlength(zero)) ///
	title("Take 5", position(12) span ///
	size(medlarge) color(black)) subtitle("") caption("") note("") ///
	legend(order(2 "{bf:3P}" 1 "{bf:2P}") rows(2) ///
	size(small) nobox region(lwidth(none)) position(11) yoffset(-2) ring(0)) ///
	plotregion(style(none)) graphregion(style(none)) subtitle("", nobox size(large) color(black))  ///
	name(fig_cpun_take5, replace)	
	
twoway (bar meancounter bar_punish if treat==1 & take==10, fcolor($color3p2) lwidth(none) barwidth(4)) ///
	(bar meancounter bar_punish if treat==2 & take==10, fcolor($color3plight) lwidth(none) barwidth(4)) ///
	(rcap hicounter10 lowcounter10 bar_punish, lcolor($color2p2)) ///
	, ytitle(Mean amount of counter-punishment) ytitle(, size(medsmall)) yscale(noline) ylabel(0(25)100, ///
	labsize(medsmall) angle(horizontal) tlength(zero) glcolor(gs12) gmin gmax)  ///
	xtitle(Level of punishment) xtitle(, size(medsmall)) xscale(noline) ///
	xlabel(0(10)100, labsize(medsmall) tlength(zero)) ///
	title("Take 10", position(12) span ///
	size(medlarge) color(black)) subtitle("") caption("") note("") ///
	legend(order(2 "{bf:3P}" 1 "{bf:2P}") rows(2) ///
	size(small) nobox region(lwidth(none)) position(11) yoffset(-2) ring(0)) ///
	plotregion(style(none)) graphregion(style(none)) subtitle("", nobox size(large) color(black))  ///
	name(fig_cpun_take10, replace)		
	
	
graph combine fig_cpun_take0 fig_cpun_take5 fig_cpun_take10, iscale(*1.25) imargin(sides) ///
	xsize(15) ysize(6) graphregion(margin(tiny) style(none)) rows(1) ///
	name(fig_cpun_take, replace)
		
***

// BARS ON COUNTER-PUNISHMENT ONLY	
use counterpunish.dta, clear
keep treat group take type punish0 punish5 punish10 counter0 counter10 counter20 counter30 counter40 counter50 counter60 counter70 counter80 counter90 counter100

gen id=_n	
reshape long counter, i(id) j(punish)
drop punish0 punish5 punish10

collapse (mean) meancounter=counter (sd) sdcounter=counter (count) n=counter if type==1, by(punish treat)

generate hicounter = meancounter + invttail(n-1,0.025)*(sdcounter / sqrt(n))
generate lowcounter = meancounter - invttail(n-1,0.025)*(sdcounter / sqrt(n))

*********  FIGURES *********
clonevar bar_punish = punish
replace bar_punish = cond(treat==2, bar_punish - 2.1, bar_punish + 2.1)
******* COLOR SCHEME *******
graph set window fontface default
set scheme s2colormanu
global color2plight = `""0 0 0 %85""' 
global color3plight = `""252 186 3""' 
global color2p = `""0 0 0""' 
global color3p = `""212 159 0""' 
global color3plight2 = `""0 131 143 %95""'
global color3p2 = `""0 131 143""'
global color2p2 = `""55 71 79""' 
****************************

twoway (bar meancounter bar_punish if treat==1, fcolor($color3p2) lwidth(none) barwidth(4)) ///
	(bar meancounter bar_punish if treat==2, fcolor($color3plight) lwidth(none) barwidth(4)) ///
	(rcap hicounter lowcounter bar_punish, lcolor($color2p2)) ///
	, ytitle(Mean amount of counter-punishment) ytitle(, size(medsmall)) yscale(noline) ylabel(0(25)100, ///
	labsize(medsmall) angle(horizontal) tlength(zero) glcolor(gs12) gmin gmax)  ///
	xtitle(Level of punishment) xtitle(, size(medsmall)) xscale(noline) ///
	xlabel(10(10)100, labsize(medsmall) tlength(zero)) ///
	title("", position(12) span ///
	size(medlarge) color(black)) subtitle("") caption("") note("") ///
	legend(order(2 "{bf:3P}" 1 "{bf:2P}") rows(2) ///
	size(small) nobox region(lwidth(none)) position(11) yoffset(-2) ring(0)) ///
	plotregion(style(none)) graphregion(style(none)) subtitle("", nobox size(large) color(black))  ///
	name(fig_cpun_only_take5_10, replace)	
		
		
******************************
*** APPENDIX NEW FIGURES 2 ***
******************************
clear
use counterpunish.dta, clear		
		
******* COLOR SCHEME *******
// graph set window fontface "Helvetica"
graph set window fontface default
set scheme s2colormanu
global color2plight = `""0 0 0 %85""' 
global color3plight = `""252 186 3""' 
global color2p = `""0 0 0""' 
global color3p = `""212 159 0""' 
global color3plight2 = `""0 131 143 %95""'
global color3p2 = `""0 131 143""'
global color2p2 = `""55 71 79""' 
****************************

graph bar if treat==1, over(take) ///
	bar(1, fcolor($color3p2) lwidth(none)) ///
	bargap(1) blabel(bar, size(medsmall) format(%9.2g)) ///
	ytitle("Proportion of responses by amount taken") ytitle(, size(medsmall)) yscale(noline) ///
	ylabel(0(30)60, labsize(medsmall) angle(horizontal) format(%9.2g) tlength(zero) glcolor(gs12) gmin gmax) ///
	title("{bf:2P}", position(12) span size(medlarge) color(black)) ///
	subtitle("") caption("") note(" ") ///
	legend(off) ///
	plotregion(style(none)) graphregion(style(none)) subtitle(" ", nobox size(large) color(black))  ///
	name(fig_take_2p_append, replace)

graph bar if treat==2, over(take) ///
	bar(1, fcolor($color3plight) lwidth(none)) ///
	bargap(1) blabel(bar, size(medsmall) format(%9.2g)) ///
	ytitle("") ytitle(, size(medsmall)) yscale(noline) ///
	ylabel(0(30)60, labsize(zero) angle(horizontal) format(%9.2g) tlength(zero) glcolor(gs12) gmin gmax) ///
	title("{bf:3P}", position(12) span size(medlarge) color(black)) ///
	subtitle("") caption("") note(" ") ///
	legend(off) ///
	plotregion(style(none)) graphregion(style(none)) subtitle(" ", nobox size(large) color(black))  ///
	name(fig_take_3p_append, replace)


graph combine fig_take_2p_append fig_take_3p_append, iscale(*1.25) imargin(sides) ///
	xsize(15) ysize(6) graphregion(margin(tiny) style(none)) ///
	name(fig_take_append, replace)
	
	
// BARS ON PUNISHMENT ONLY	
use counterpunish.dta, clear
keep treat group type take0 take5 take10 punish0 punish5 punish10 
keep if type==1

gen id=_n	
reshape long punish, i(id) j(take)

drop take0 take5 take10

collapse (mean) meanpunish=punish (sd) sdpunish=punish (count) n=punish if type==1, by(take treat)

generate hipunish = meanpunish + invttail(n-1,0.025)*(sdpunish / sqrt(n))
generate lowpunish = meanpunish - invttail(n-1,0.025)*(sdpunish / sqrt(n))

*********  FIGURES *********
clonevar bar_take = take
replace bar_take = cond(treat==2, bar_take - 1.02, bar_take + 1.02)
******* COLOR SCHEME *******
graph set window fontface default
set scheme s2colormanu
global color2plight = `""0 0 0 %85""' 
global color3plight = `""252 186 3""' 
global color2p = `""0 0 0""' 
global color3p = `""212 159 0""' 
global color3plight2 = `""0 131 143 %95""'
global color3p2 = `""0 131 143""'
global color2p2 = `""55 71 79""' 
****************************

twoway (bar meanpunish bar_take if treat==1, fcolor($color3p2) lwidth(none) barwidth(2)) ///
	(bar meanpunish bar_take if treat==2, fcolor($color3plight) lwidth(none) barwidth(2)) ///
	(rcap hipunish lowpunish bar_take, lcolor($color2p2)) ///
	, ytitle(Mean punishment in % terms) ytitle(, size(medsmall)) ///
	yscale(noline) ylabel(0(10)50, labsize(medsmall) angle(horizontal) tlength(zero) ///
	glcolor(gs12) gmin gmax) xtitle(Amount taken) xtitle(, size(medsmall)) xscale(noline) ///
	xlabel(0(5)10, labsize(medsmall) tlength(zero)) ///
	title("", position(12) span size(medlarge) ///
	color(black)) subtitle("") caption("") note("") ///
	legend(order(2 "{bf:3P}" 1 "{bf:2P}") rows(2) ///
	size(small) nobox region(lwidth(none)) position(11) yoffset(-2) ring(0)) ///
	plotregion(style(none)) graphregion(style(none)) subtitle("", nobox size(large) color(black))  ///	
	name(fig_pun_only_all, replace) 
		
	
********************************************************************************
				*** APPENDDIX: EVOLUTION OF EARNINGS ***
********************************************************************************
use counterpunish.dta, clear

gen pay_1=.
	replace pay_1=30+exec_take if type==1
	replace pay_1=30-exec_take if type==2

gen pay_2=.
	replace pay_2=((100-exec_punish)/100)*pay_1 if type==1
	replace pay_2=pay_1+5 if type==2 & exec_punish==0
	replace pay_2=pay_1 if type==2 & exec_punish!=0
	
gen pay_3=.
	replace pay_3=((100-exec_counter)/100)*pay_2 if type==2
	replace pay_3=pay_2+5 if type==1 & exec_counter==0
	replace pay_3=pay_2 if type==1 & exec_counter!=0
		
	
forvalues i=1(1)3{
gen tmp_pay_a_`i' = pay_`i' if type==1	
gen tmp_pay_b_`i' = pay_`i' if type==2
egen pay_a_`i' = max(tmp_pay_a_`i'), by(treat group)
egen pay_b_`i' = max(tmp_pay_b_`i'), by(treat group)
gen rel_pay_`i' = pay_a_`i' / pay_b_`i'
drop tmp_pay_a_`i' tmp_pay_b_`i'
}
	
bysort treat type: sum pay_1 pay_2 pay_3

bysort exec_take: reg pay_1 i.treat##i.type, cl(group)
bysort exec_take: reg pay_2 i.treat##i.type, cl(group)
bysort exec_take: reg pay_3 i.treat##i.type, cl(group)


collapse pay_a_1 pay_b_1 pay_a_2 pay_b_2 pay_a_3 pay_b_3 rel_pay_*, by(treat exec_take)
sort treat exec_take
gen id=_n
sort id
reshape long pay_a_ pay_b_ rel_pay_, i(id) j(stage)
ren pay_a_ pay_a
ren pay_b_ pay_b
ren rel_pay_ rel_pay
sort treat exec_take stage

la var exec_take "Take"
la def exec_takelbl 0 "Take 0" 5 "Take 5" 10 "Take 10"
la val exec_take exec_takelbl

sort exec_take stage treat pay* rel*

******* COLOR SCHEME *******
graph set window fontface default
set scheme s2colormanu
global color2plight = `""0 0 0 %85""' 
global color3plight = `""252 186 3""' 
global color2p = `""0 0 0""' 
global color3p = `""212 159 0""' 
global color3plight2 = `""0 131 143 %95""'
global color3p2 = `""0 131 143""'
global color2p2 = `""55 71 79""' 
****************************

use counterpunish.dta, clear
gen endowment = 30

gen punish = .
replace punish = 0 if take == 0 & treat == 1
replace punish = 10 if take == 0 & treat == 2
replace punish = 13.5 if take == 5 & treat == 1
replace punish = 17.8 if take == 5 & treat == 2
replace punish = 26.6 if take == 10 & treat == 1
replace punish = 27.8 if take == 10 & treat == 2

gen counter = .
forvalues i=0(10)100{
	replace counter = counter`i' if take == 0 & punish0 == `i'
	replace counter = counter`i' if take == 5 & punish5 == `i'
	replace counter = counter`i' if take == 10 & punish10 == `i'
}


gen pay_1 = .
replace pay_1 = endowment + take if type == 1
replace pay_1 = endowment - take if type == 2

gen pay_2 = .
replace pay_2 = ((100-punish)/100)*pay_1 if type==1
replace pay_2=pay_1+5 if type==2 & punish==0
replace pay_2=pay_1 if type==2 & punish!=0
	
gen pay_3=.
	replace pay_3=((100-counter)/100)*pay_2 if type==2
	replace pay_3=pay_2+5 if type==1 & counter==0
	replace pay_3=pay_2 if type==1 & counter!=0
		
	
forvalues i=1(1)3{
gen tmp_pay_a_`i' = pay_`i' if type==1	
gen tmp_pay_b_`i' = pay_`i' if type==2
egen pay_a_`i' = max(tmp_pay_a_`i'), by(treat group)
egen pay_b_`i' = max(tmp_pay_b_`i'), by(treat group)
gen rel_pay_`i' = pay_a_`i' / pay_b_`i'
drop tmp_pay_a_`i' tmp_pay_b_`i'
}
	
bysort treat type: sum pay_1 pay_2 pay_3

bysort exec_take: reg pay_1 i.treat##i.type, cl(group)
bysort exec_take: reg pay_2 i.treat##i.type, cl(group)
bysort exec_take: reg pay_3 i.treat##i.type, cl(group)


collapse pay_a_1 pay_b_1 pay_a_2 pay_b_2 pay_a_3 pay_b_3 rel_pay_*, by(treat exec_take)
sort treat exec_take
gen id=_n
sort id
reshape long pay_a_ pay_b_ rel_pay_, i(id) j(stage)
ren pay_a_ pay_a
ren pay_b_ pay_b
ren rel_pay_ rel_pay
sort treat exec_take stage

la var exec_take "Take"
la def exec_takelbl 0 "Take 0" 5 "Take 5" 10 "Take 10"
la val exec_take exec_takelbl

sort exec_take stage treat pay* rel*

******* COLOR SCHEME *******
graph set window fontface default
set scheme s2colormanu
global color2plight = `""0 0 0 %85""' 
global color3plight = `""252 186 3""' 
global color2p = `""0 0 0""' 
global color3p = `""212 159 0""' 
global color3plight2 = `""0 131 143 %95""'
global color3p2 = `""0 131 143""'
global color2p2 = `""55 71 79""' 
****************************


twoway (connected pay_a stage if treat==1, mcolor($color3plight) msymbol(circle) lcolor($color3plight)) ///
	(connected pay_a stage if treat==2, mcolor($color3p2) msymbol(triangle) lcolor($color3p2)), ///
	ytitle(Earnings Player A) ytitle(, size(medsmall)) ///
	yscale(noline) ylabel(0(20)40, labsize(medsmall) angle(horizontal) labgap(zero) tlength(zero) gmin gmax) ///
	xtitle(Stages) xtitle(, size(medsmall)) xscale(noline) ///
	xlabel(1 "Take" 2 "Pun" 3 "CPun", labsize(small) angle(forty_five) tlength(zero)) ///
	by(, title("") subtitle("") caption("") note("")) ///
	legend(order(1 "2P " 2 "3P") rows(1) size(small) nobox region(lwidth(none))) ///
	by(exec_take, rows(1)) subtitle(, size(medlarge) nobox) ///
	name(fig_earnings_a_append, replace)


twoway (connected pay_b stage if treat==1, mcolor($color3plight) msymbol(circle) lcolor($color3plight)) ///
	(connected pay_b stage if treat==2, mcolor($color3p2) msymbol(triangle) lcolor($color3p2)), ///
	ytitle(Earnings Player B) ytitle(, size(medsmall)) ///
	yscale(noline) ylabel(0(20)40, labsize(medsmall) angle(horizontal) labgap(zero) tlength(zero) gmin gmax) ///
	xtitle(Stages) xtitle(, size(medsmall)) xscale(noline) ///
	xlabel(1 "Take" 2 "Pun" 3 "CPun", labsize(small) angle(forty_five) tlength(zero)) ///
	by(, title("") subtitle("") caption("") note("")) ///
	legend(order(1 "2P " 2 "3P") rows(1) size(small) nobox region(lwidth(none))) ///
	by(exec_take, rows(1)) subtitle(, size(medlarge) nobox) ///
	name(fig_earnings_b_append, replace)
	
twoway (connected rel_pay stage if treat==1, mcolor($color3plight) msymbol(circle) lcolor($color3plight)) ///
	(connected rel_pay stage if treat==2, mcolor($color3p2) msymbol(triangle) lcolor($color3p2)), ///
	ytitle("Relative Earnings (A/B)") ytitle(, size(medsmall)) ///
	yscale(noline) ylabel(0(.5)2.5, labsize(medsmall) angle(horizontal) labgap(zero) tlength(zero) gmin gmax) ///
	xtitle(Stages) xtitle(, size(medsmall)) xscale(noline) ///
	xlabel(1 "Take" 2 "Pun" 3 "CPun", labsize(small) angle(forty_five) tlength(zero)) ///
	by(, title("") subtitle("") caption("") note("")) ///
	legend(order(1 "2P " 2 "3P") rows(1) size(small) nobox region(lwidth(none))) ///
	by(exec_take, rows(1)) subtitle(, size(medlarge) nobox) ///
	name(fig_earnings_relative_append, replace)	
	
graph combine fig_earnings_a_append fig_earnings_b_append fig_earnings_relative_append, iscale(*1.25) imargin(sides) ///
	xsize(15) ysize(6) graphregion(margin(tiny) style(none)) rows(1) ///
	name(fig_earnings_append, replace)
			

********************************************************************************
exit, clear
********************************************************************************
